package com.tzeng.adapter;

import java.util.List;
import java.util.Map;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.download.ImageDownloader.Scheme;
import com.tzeng.hist.R;
import com.tzeng.hist.manager.ImageManager;

public class NewsAdapter extends BaseAdapter {

	List<Map<String, Object>> data;
	Context c;
	private LayoutInflater inflater;
	protected ImageLoader imageLoader = ImageLoader.getInstance();

	@Override
	public void notifyDataSetChanged() {
		super.notifyDataSetChanged();
	}

	public NewsAdapter(Context context, List<Map<String, Object>> data) {
		inflater = LayoutInflater.from(context);
		this.c = context;
		this.data = data;

	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return data.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return data.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView == null) {
			holder = new ViewHolder();
			/* 绑定相应的视图 */
			convertView = inflater.inflate(R.layout.item_news, null);
			holder.img = (ImageView) convertView
					.findViewById(R.id.img_news_head);
			holder.title = (TextView) convertView
					.findViewById(R.id.tv_news_title);
			holder.info = (TextView) convertView
					.findViewById(R.id.tv_news_info);
			holder.time = (TextView) convertView
					.findViewById(R.id.tv_news_time);
			holder.clicknum = (TextView) convertView
					.findViewById(R.id.tv_news_clicknum);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		holder.title.setText(data.get(position).get("news_title").toString());
		holder.info.setText(data.get(position).get("news_info").toString());
		holder.time.setText(data.get(position).get("news_time").toString());
		holder.clicknum.setText(data.get(position).get("news_clicknum")
				.toString());
		// holder.img.setImageResource((Integer)data.get(position).get("news_head"));
		// TODO 异步加载图片
		//图片来源于assets
        String assetsUrl = Scheme.ASSETS.wrap(data.get(position).get("news_head").toString());
		ImageManager.getInstance().displayImage(assetsUrl, holder.img,
				ImageManager.getNewsHeadOptions());

		return convertView;

	}

	/**
	 * 自定义类 绑定视图
	 * 
	 * @author zwt
	 */
	final class ViewHolder {
		public ImageView img;
		public TextView title;
		public TextView info;
		public TextView time;
		public TextView clicknum;
	}

}
